/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    15 Aug 2022 - 11:00:47
Modification Date:   
Do-file version:    01
References:          
Output:           Table 2  
==================================================*/

/*==================================================
			0: Program set up
==================================================*/
*Written on STATA 17
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir4r}" // tables dir

/*==================================================
			1: Overall prep
==================================================*/

*---------- 1.2: Relevant macros
// All stat variables in survey (source SISBEN, we just restrict the sample)
glo t2_1 age_feb20 no_edu elementary highschool tertiary_edu works_0 ///
formal_work n_members urban pp_inc  own_fridge own_washing_machine own_PC own_moto own_tractor own_car

// Shares assets with sisben 
glo t2_1b age_feb20 no_edu elementary highschool tertiary_edu works_0 ///
formal_work n_members urban pp_inc own_fridge own_washing_machine own_PC own_moto own_tractor own_car

/*==================================================
			2: Table construction
==================================================*/

*============= 2.1: Survey sample
use "${dir3r}/02_admin/SISBEN_framing_sample.dta", clear

*---------- 2.1.1: House keeping 

lab var age_feb20 		"Age (household head)"
*lab var female		 	"Women (household head)"	
lab var no_edu 		"Educational attainment: none"	
lab var elementary 	"Educational attainment: Elementary"		
lab var highschool 	"Educational attainment: Secondary"		
lab var tertiary_edu 	"Educational attainment: Terciary"			
lab var works_0 		"Works"
lab var formal_work	"Formal work"		 
lab var n_members 		"\# of household members"
lab var urban 			"Urban"
lab var pp_inc			"Per-capita Income (1000s of \$ CPO)"
lab var own_fridge "Owns a fridge"
lab var own_washing_machine "Owns a washing machine"
lab var own_PC "Owns a computer"
lab var own_moto "Owns a motorcycle"
lab var own_tractor "Owns a tractor"
lab var own_car "Owns a car"


//percaita price adjustment 
** pending on see the date of collection, but temporal fix
*gen deflactor = 1.104515314 // feb 2017 to feb 2020 prices
gen deflactor = 1.0995004 // feb 2017 to Nov 2019 prices

replace pp_inc = pp_inc * deflactor/1000

// Declare a global used variables  
glo covs $t2_1

*---------- 2.1.2: Gen column 

// estimation 
loc j = 0
loc rowname ""				// An empty local to keep track of var/names
foreach v of global covs { 	
loc ++j

sum `v' [aw=pondera] if survey_sample == 1
scalar v_mean = r(mean)
scalar v_min = r(min)
scalar v_max = r(max)
scalar v_sd = r(sd)
scalar v_N = r(N)


// store data in matrix
*matrix output = J(1,4,.)
matrix output = J(1,2,.)
matrix output[1,1] = v_mean
matrix output[1,2] = v_sd
*matrix output[1,3] = v_min
*matrix output[1,4] = v_max

if (`j' == 1) 		mat balance = output
else 				mat balance = balance\output

loc rowname "`rowname' `v'"
*loc hline = "`hline'001"
loc hline = "`hline'01"
}

matrix rownames balance = `rowname'
matrix list balance 
di "`hline'"

count if survey_sample == 1
scalar obs = r(N)

mat balance1 = balance

// export table with smart looks
matrix dcols = (0,0,0,1)
frmttable using tablaA1 , statmat(balance) ctitles("", "Surveyed sample") ///
substat(1)  sdec(2) varlabels ///
hlines(01`hline') vlines(01)  basefont(plain  fs11) coljust(c) /// 
addrows("","" \ "Observations",  "`=scalar(obs)'")  tex  frag    /// add a row with the number of observations
replace 


/*LATEX
frmttable using Table1 , statmat(stat) ///
ctitles( "Panel B: Household characteristics" ) ///
sdec(2) varlabels coljust(l c) ///
hlines(01{0}) addtable tex frag */

*============= 2.1: SISBEN
use "${dir3r}/02_admin/SISBEN_foruse.dta", clear

*---------- 2.1.1: House keeping 
lab var age_feb20 		"Age (household head)"
*lab var female		 	"Women (household head)"	
lab var no_edu 		"Educational attainment: none"	
lab var elementary 	"Educational attainment: Elementary"		
lab var highschool 	"Educational attainment: Secondary"		
lab var tertiary_edu 	"Educational attainment: Terciary"			
lab var works_0 		"Works"
lab var formal_work	"Formal work"		 
lab var n_members 		"\# of household members"
lab var urban 			"Urban"
lab var pp_inc			"Per-capita Income (1000s of \$ CPO)"
lab var own_fridge "Owns a fridge"
lab var own_washing_machine "Owns a washing machine"
lab var own_PC "Owns a computer"
lab var own_moto "Owns a motorcycle"
lab var own_tractor "Owns a tractor"
lab var own_car "Owns a car"


//percaita price adjustment 
gen deflactor = 1.0995004 // feb 2017 to Nov 2019 prices

replace pp_inc = pp_inc * deflactor/1000

// Declare a global used variables  
glo covs $t2_1b

*---------- 2.1.2: Gen column 

// estimation 
loc j = 0
loc hline ""
loc rowname ""				// An empty local to keep track of var/names
foreach v of global covs { 	
loc ++j

sum `v'
scalar v_mean = r(mean)
scalar v_min = r(min)
scalar v_max = r(max)
scalar v_sd = r(sd)
scalar v_N = r(N)


// store data in matrix
*matrix output = J(1,4,.)
matrix output = J(1,2,.)
matrix output[1,1] = v_mean
matrix output[1,2] = v_sd
*matrix output[1,3] = v_min
*matrix output[1,4] = v_max

if (`j' == 1) 		mat balance = output
else 				mat balance = balance\output

loc rowname "`rowname' `v'"
*loc hline = "`hline'001"
loc hline = "`hline'01"
}

matrix rownames balance = `rowname'
matrix list balance 
di "`hline'"

count 
scalar obs = r(N)

// export table with smart looks
matrix dcols = (0,0,0,1)
frmttable using TA1_Summary_Stats, tex  frag   statmat(balance) ctitles("", "All") ///
substat(1) /*doubles(dcols)*/ sdec(2) varlabels ///
hlines(01`hline') vlines(01) basefont(plain  fs11) coljust(c) /// 
addrows("","" \ "Observations",  "`=scalar(obs)'") /// add a row with the number of observations
merge 




/* End of do-file */

